import { request } from "@apis/request";
import { message } from 'antd';
import * as types from './actionTypes';

/**
 * 同步action
 */
const addPrevious = obj => ({ type: types.ADD_PREVIOUS, data: obj });
const getPrevious = list => ({ type: types.GET_PREVIOUS, data: list });
const delPrevious = obj => ({ type: types.DEL_PREVIOUS, data: obj });
const editPrevious = obj => ({ type: types.EDIT_PREVIOUS, data: obj });

/**
 * 异步action 
 */

// 添加往期功能
export const addPreviousAction = (content, contriId, navigate) => {
  return async dispatch => {
    const res = await request('/addPreviousFonc', { content, contriId }, 'post');
    console.log('res: ', res)
    if (res.code === '0000') {
      dispatch(addPrevious(res.data));
      navigate('/message-board/previous-fonc', { replace: true });
    } else {
      message.error(res.message);
    }
  }
}

// 获取往期功能
export const getPreviousAction = () => {
  return async dispatch => {
    const res = await request('/previousList');
    if (res.code === '0000') {
      dispatch(getPrevious(res.data));
    }
  }
}

// 删除往期功能
export const deletePreviousAction = (id) => {
  return async dispatch => {
    const res = await request('/deletePreviosItem', { id }, 'post');
    if (res.code === '0000') {
      dispatch(delPrevious(res.data));
      message.success('删除成功');
    } else {
      message.error(res.message);
    }
  }
}

// 编辑往期功能
export const editPreviousAction = (id, content, contriId, navigate) => {
  return async dispatch => {
    const res = await request('/editPrevios', { id, content, contriId }, 'post');
    if (res.code === '0000') {
      dispatch(editPrevious(res.data));
      message.success('修改成功');
      navigate('/message-board/previous-fonc', { replace: true });
    } else {
      message.error(res.message);
    }
  }
}

// 增加研发项目
const addDevItem = obj => ({ type: types.ADD_DEV_ITEM, data: obj });
export const addDevItemAction = (content, contriId, devProg, navigate) => {
  return async dispatch => {
    const res = await request('/addDevItem', { content, contriId, devProg }, 'post');
    if (res.code === '0000') {
      dispatch(addDevItem(res.data));
      navigate('/message-board/under-development', { replace: true });
    } else {
      message.error(res.message);
    }
  }
}

// 删除研发项目
const delDevItem = obj => ({ type: types.DEL_DEV_ITEM, data: obj });
export const delDevItemAction = (id) => {
  return async dispatch => {
    const res = await request('/deleteDevItem', { id }, 'post');
    if (res.code === '0000') {
      dispatch(delDevItem(res.data));
    } else {
      message.error(res.message);
    }
  }
}

// 修改研发项目
const reviseDevItem = obj => ({ type: types.REVISE_DEV_ITEM, data: obj });
export const reviseDevItemAction = (id, content, contriId, devProg, navigate) => {
  return async dispatch => {
    const res = await request('/reviseDevItem', { id, content, contriId, devProg }, 'post');
    if (res.code === '0000') {
      dispatch(reviseDevItem(res.data));
      navigate('/message-board/under-development', { replace: true });
      addPreviousAction()
    } else {
      message.error(res.message);
    }
  }
}

// 获取研发项目列表
const getDevItemList = list => ({ type: types.GET_DEV_ITEM_LIST, data: list });
export const getDevItemListAction = () => {
  return async dispatch => {
    const res = await request('/devItemList');
    if (res.code === '0000') {
      dispatch(getDevItemList(res.data));
    } else {
      console.log(res.message);
    }
  }
}

// 发布研发项目
const publishDevItem = obj => ({ type: types.PUBLISH_DEV_ITEM, data: obj });
export const publishDevItemAction = (id, navigate) => {
  return async dispatch => {
    const res = await request('/publishDevItem', { id }, 'post');
    if (res.code === '0000') {
      dispatch(publishDevItem(res.data));
      navigate('/message-board/previous-fonc', { replace: true });
    } else {
      message.error(res.message);
    }
  }
}

// 按照搜索条件获取留言列表
const getLeaveMessageList = list => ({ type: types.GET_LEAVE_MESSAGE_LIST, data: list });
export const getLeaveMessageListAction = (user, isAccepted, startDate, endDate) => {
  return async dispatch => {
    const res = await request('/searchUserLeaveMessages', { user, isAccepted, startDate, endDate });
    if (res.code === '0000') {
      dispatch(getLeaveMessageList(res.data));
    } else {
      console.log('error: ', res.message);
    }
  }
}

// 删除留言
const delOneLeave = obj => ({ type: types.DEL_ONE_LEAVE_MESSAGE, data: obj });
export const delOneLeaveAction = (id) => {
  return async dispatch => {
    const res = await request('/deleteOneLeaveMessage', { id }, 'post');
    if (res.code === '0000') {
      message.success('删除成功');
      dispatch(delOneLeave(res.data));
    } else {
      message.error(res.message);
    }
  }
}

// 采纳留言
const acceptLeave = obj => ({ type: types.ADD_ACCEPT_LEAVE, data: obj });
export const acceptLeaveAction = (id) => {
  return async dispatch => {
    const res = await request('/acceptLeaveMessage', { id }, 'post');
    if (res.code === '0000') {
      message.success('采纳成功');
      dispatch(acceptLeave(res.data));
    } else {
      message.error(res.message);
    }
  }
}

// 取消采纳留言
const cancelAccept = obj => ({ type: types.CANCEL_ACCEPT_LEAVE, data: obj });
export const cancelAcceptAction = (id) => {
  return async dispatch => {
    const res = await request('/noAcceptLeaveMessage', { id }, 'post');
    if (res.code === '0000') {
      message.success('取消成功');
      dispatch(cancelAccept(res.data));
    } else {
      message.error(res.message);
    }
  }
}